package com.jing.sheng;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    private   WebView webView;
    @SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

//        WebView webView = findViewById(R.id.webview);
//        webView.getSettings().setJavaScriptEnabled(true);  // 如果HTML中包含JavaScript，需要启用JavaScript
//        webView.loadUrl("file:///android_asset/sample.html");
        webView = findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new WebChromeClient());
        webView.addJavascriptInterface(new WebAppInterface(this), "Android");
        webView.loadUrl("file:///android_asset/sample.html");

        Button btn=findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("ok");

                webView.evaluateJavascript("javascript:myJavaScriptFunction()", new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {
                        // 处理 JavaScript 代码执行的结果
                        System.out.println(value);
                        Toast.makeText(MainActivity.this, value, Toast.LENGTH_SHORT).show();
                    }
                });

                loadJavaScriptToGetTableData();
            }
        });
    }
    // 加载JavaScript代码以获取表格数据
    private void loadJavaScriptToGetTableData() {
        webView.evaluateJavascript(
                "(function() {" +
                        "   var tableData = [];" +
                        "   var table = document.getElementsByTagName('table')[0];" +
                        "   var rows = table.getElementsByTagName('tr');" +
                        "   for (var i = 0; i < rows.length; i++) {" +
                        "       var cells = rows[i].getElementsByTagName('td');" +
                        "       var rowData = [];" +
                        "       for (var j = 0; j < cells.length; j++) {" +
                        "           var input = cells[j].getElementsByTagName('input')[0];" +
                        "           rowData.push(input.value);" + // 使用 input.value 获取输入框的值
                        "       }" +
                        "       tableData.push(rowData);" +
                        "   }" +
                        "   return JSON.stringify(tableData);" +
                        "})()",
                new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {
                        // 在这里处理JavaScript代码返回的表格数据
                        String tableDataJson = value;
                        Toast.makeText(MainActivity.this, "Table Data: " + tableDataJson, Toast.LENGTH_SHORT).show();
                        System.out.println(tableDataJson);
                    }
                }
        );
    }
    public class WebAppInterface {
        private MainActivity mContext;

        WebAppInterface(MainActivity c) {
            mContext = c;
        }
        @JavascriptInterface
        public void showToast(String message) {
            //这个注解允许在 WebView 中运行的 JavaScript 调用这个方法。
            Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.destroy();
    }
}
